package com.woniuxy.joylifeshop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.entity.ShopComment;
import com.woniuxy.common.utils.JWTUtils;
import com.woniuxy.joylifeshop.service.CenterUserService;
import com.woniuxy.joylifeshop.service.CommentService;
import com.woniuxy.joylifeshop.vo.ResponseInformation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(value = "",description = "joylifeShop's API")
@RestController
public class CommentController {

    @Autowired
    CommentService commentService;

    @Autowired
    CenterUserService centerUserService;

    /**
    * @Author SooYoo
    * @Description 提交评论
    * @Param
    * @Return
    **/
    @RequestMapping("/commitComment")
    public ResponseInformation commitComment(String shopId, String content, HttpServletRequest request){

        System.out.println(shopId);

        String userId = JWTUtils.getUserId(request.getHeader("token"));
        ResponseInformation responseInformation = new ResponseInformation();

        System.out.println(shopId+content);
        //验证content的空
        if (content==""){

            responseInformation.setState(202);
            responseInformation.setMsg("评论不得为空");

            return responseInformation;
        }else{
            commentService.addComment(userId,shopId,content);

            responseInformation.setData(shopId);
            responseInformation.setMsg("评论成功");
            responseInformation.setState(200);

            return responseInformation;
        }
    }


    /**
    * @Author SooYoo
    * @Description 初始化某商品的所有的评论消息
    * @Param
    * @Return
    **/

    @ApiOperation(value = "/initAllComment",notes = "Test this API")
    @RequestMapping("/initAllComment")
    public PageInfo initAllComment(String shopId,String pageNumber,String pageSize){
        //pageHelper的插件对象
        PageHelper.startPage(Integer.parseInt(pageNumber),Integer.parseInt(pageSize));

        //这个商品的评论对象集合
        List<ShopComment> shopComments = commentService.readAllCommentByComid(shopId);

        //放入对象
        PageInfo pageInfo = new PageInfo(shopComments);

        return pageInfo;
    }


        /**
        * @Author SooYoo
        * @Description 初始化评论区
        * @Param
        * @Return
        **/
        @RequestMapping("/initcomment")
        public ResponseInformation initcomment(String shopId){
            ResponseInformation responseInformation = new ResponseInformation();

            try {
                List<ShopComment> shopComments = commentService.readAllCommentByComid(shopId);

                responseInformation.setMsg("init成功");
                responseInformation.setState(200);
                responseInformation.setData(shopComments);

                return responseInformation;
            } catch (Exception e) {
                e.printStackTrace();
                responseInformation.setState(500);
                responseInformation.setMsg("init失败");

                return responseInformation;
            }
        }



}
